package com.ruoyix.weblog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.helper.LoginHelper;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.weblog.domain.bo.BlogNoteBo;
import com.ruoyix.weblog.domain.vo.BlogNoteVo;
import com.ruoyix.weblog.domain.BlogNote;
import com.ruoyix.weblog.mapper.BlogNoteMapper;
import com.ruoyix.weblog.service.IBlogNoteService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 笔记Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@RequiredArgsConstructor
@Service
public class BlogNoteServiceImpl implements IBlogNoteService {

    private final BlogNoteMapper baseMapper;

    /**
     * 查询笔记
     */
    @Override
    public BlogNoteVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询笔记列表
     */
    @Override
    public TableDataInfo<BlogNoteVo> queryPageList(BlogNoteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogNote> lqw = buildQueryWrapper(bo);
        Page<BlogNoteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询笔记列表
     */
    @Override
    public List<BlogNoteVo> queryList(BlogNoteBo bo) {
        LambdaQueryWrapper<BlogNote> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogNote> buildQueryWrapper(BlogNoteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogNote> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, BlogNote::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getArticleTitle()), BlogNote::getArticleTitle, bo.getArticleTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getArticleContent()), BlogNote::getArticleContent, bo.getArticleContent());
        lqw.eq(StringUtils.isNotBlank(bo.getNoteType()), BlogNote::getNoteType, bo.getNoteType());
        lqw.eq(bo.getViewStatus() != null, BlogNote::getViewStatus, bo.getViewStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), BlogNote::getCreateBy, bo.getCreateBy());
        lqw.orderByDesc(BlogNote::getUpdateTime) // 先按修改时间降序
            .orderByDesc(BlogNote::getCreateTime);
        return lqw;
    }

    /**
     * 新增笔记
     */
    @Override
    public Boolean insertByBo(BlogNoteBo bo) {
        BlogNote add = BeanUtil.toBean(bo, BlogNote.class);
        validEntityBeforeSave(add);
        add.setUserId(LoginHelper.getUserId());
        add.setNoteType(bo.getNoteType());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改笔记
     */
    @Override
    public Boolean updateByBo(BlogNoteBo bo) {
        BlogNote update = BeanUtil.toBean(bo, BlogNote.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogNote entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除笔记
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
